package com.example.vue3.controller;

import com.example.vue3.model.ResultData;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangMaoXiong
 * @version 1.0
 * @date 2022/12/26 15:59
 */
@RestController
public class VueController {

    /**
     * http://localhost:8080/vue/loadPersons?deptNo=10
     *
     * @param deptNo
     * @return
     */
    @GetMapping("/vue/loadPersons")
    public ResultData loadPersons(@RequestParam Integer deptNo) {
        List<Map<String, Object>> dataList = getDataList(deptNo);
        return new ResultData(dataList);
    }

    @PostMapping("/vue/loadPersons")
    public ResultData loadPersons(@RequestParam Integer deptNo,
                                  @RequestParam String version,
                                  @RequestBody List<Map<String, Object>> body) {
        System.out.println(deptNo);
        System.out.println(version);
        System.out.println(body);
        List<Map<String, Object>> dataList = getDataList(deptNo);
        return new ResultData(dataList);
    }

    private List<Map<String, Object>> getDataList(@RequestParam Integer deptNo) {
        List<Map<String, Object>> dataList = new ArrayList<>();
        Map<String, Object> dataMap1 = new HashMap<>(8);
        Map<String, Object> dataMap2 = new HashMap<>(8);
        Map<String, Object> dataMap3 = new HashMap<>(8);

        dataMap1.put("name", "张三");
        dataMap1.put("age", 16);
        dataMap1.put("deptNo", deptNo);

        dataMap2.put("name", "李四");
        dataMap2.put("age", 25);
        dataMap2.put("deptNo", deptNo);

        dataMap3.put("name", "王五");
        dataMap3.put("age", 32);
        dataMap3.put("deptNo", deptNo);

        dataList.add(dataMap1);
        dataList.add(dataMap2);
        dataList.add(dataMap3);
        return dataList;
    }


}
